package arraylist03;

import java.util.ArrayList;

/**
 * ArrayList中存储自定义引用类型
 *   这个集合中存储什么元素类型,那么泛型就指定为该类型
 */
public class ArrayListDemo01 {
    public static void main(String[] args) {
        ArrayList<Person> people = new ArrayList<Person>();//我们想利用这个people集合存储人,所以把泛型定义为Person类型

        Person p1 = new Person("李逍遥", 18);
        Person p2 = new Person("赵灵儿", 17);
        Person p3 = new Person("林月如", 19);

        people.add(p1); //将这三个人添加集合中
                        //Object[] elementData=new Object[10]
                        // elementData[0]= p1 =new Person("李逍遥", 18); //elementData[0]最终指向李逍遥对象
        people.add(p2); //elementData[1] = p2 =new Person("赵灵儿", 17);//elementData[1]最终指向赵灵儿对象
        people.add(p3);//elementData[2] = p3 =new Person("林月如", 19);//elementData[2]最终指向林月如对象




        for(Person p : people){ //第一次循环 Person p = p1 = new Person("李逍遥", 18)
                               //相当于 Person p =  new Person("李逍遥", 18);
                               //第二次循环 Person p = p2 = new Person("赵灵儿", 17);
                                //相当于 Person p =  new Person("赵灵儿", 17);
                               //第三次循环 Person p = p3 = new Person("林月如", 19);
                               //相当于 Person p =   new Person("林月如", 19);
            String name = p.getName();
            int age = p.getAge();
            System.out.println(name+" "+age);
        }


    }
}
